import React, { useState, useEffect } from 'react';
import {
    Button,
    Cascader,
    DatePicker,
    Form,
    Input,
    InputNumber,
    Radio,
    Select,
    Switch,
    TreeSelect,
} from 'antd';
import type { hrlistTS } from '../../store/crmsTS';
import { async_addlist, async_updatelist } from '../../store/crmsSlice';
import { useAppDispatch } from '../../store/reactreduxts';

interface Iprops {
    title: string
    itemobj: {} | hrlistTS
    handleOk: () => void
}

const Myfrom: React.FC<Iprops> = (props) => {


    let dispatch = useAppDispatch()
    //表单验证时的回调
    const [form] = Form.useForm();
    useEffect(() => {
        form.setFieldsValue(props.itemobj)
    }, [])

    const onFinish = (values: any) => {
        //添加
        if (props.title == '添加') {
            let obj = {
                ...values,
                id: new Date().getTime()
            }
            //执行添加的方法
            dispatch(async_addlist(obj))
        } else {
            let obj = {
                ...values,
                id: props.itemobj.id
            }
            //执行编辑的方法
            dispatch(async_updatelist(obj))
        }
        //添加或编辑完成 关闭弹框
        props.handleOk()
    };

    return (
        <Form onFinish={onFinish} form={form}>

            <Form.Item label="岗位编号" name='bianhao'
                rules={[{ required: true, message: '请输入岗位编号!' }]}>
                <Input />
            </Form.Item>

            <Form.Item label="岗位编码" name='bianma'
                rules={[{ required: true, message: '请输入岗位编码!' }]}>
                <Input />
            </Form.Item>
            <Form.Item label="岗位名称" name='name'
                rules={[{ required: true, message: '请输入岗位名称!' }]}>
                <Input />
            </Form.Item>

            <Form.Item label="岗位排序" name='paixu'
                rules={[{ required: true, message: '请输入显示排序!' }]}>
                <InputNumber />
            </Form.Item>

            <Form.Item label="部门状态" name='collect'>
                <Radio.Group
                    options={[
                        { value: true, label: '正常' },
                        { value: false, label: '异常' }
                    ]}
                />
            </Form.Item>

            <Form.Item>
                <Button type='primary' htmlType="submit">完成</Button>
            </Form.Item>
        </Form>
    );
};

export default Myfrom;